package model

import (
	"time"
)

type EverydayElectrity struct {
	Id int64 `xorm:"pk autoincr"`
	DayTime string
	RoomId int64
	Electrity float64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Room struct {
	Id int64 `xorm:"pk autoincr"`
	Number string
	YesterdaysDegree float64
	TodayDegree float64
	CreatedAt time.Time
	UpdatedAt time.Time
}

func GenerateElectrity()(map[string]interface{}, error){

	var rList []*Room
	x.Find(&rList)
	for _, rv := range rList{
		var ee = EverydayElectrity{
			DayTime: time.Now().Format("2006-01-02"),
			RoomId: rv.Id,
			Electrity: 1.5* (rv.TodayDegree - rv.YesterdaysDegree),
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		x.Insert(&ee)

		rv.YesterdaysDegree = rv.TodayDegree
		rv.TodayDegree = 0
		x.Id(rv.Id).Update(&rv)
	}

	return map[string]interface{}{"code":200, "message": "成功"}, nil
}

